package com.mixpanel.example.hello.custom;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.Button;

import com.mixpanel.example.hello.R;

/**
 * @Copyright © 2018 EGuan Inc. All rights reserved.
 * @Description: TODO
 * @Version: 1.0
 * @Create: 2018/4/20 15:11
 * @Author: sanbo
 */
@SuppressLint("AppCompatCustomView")
public class CuButton extends Button {
    private int resourceId = 0;
    private Bitmap bitmap;

    public CuButton(Context context) {
        super(context, null);
    }

    public CuButton(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.setClickable(true);
        resourceId = R.drawable.icon;
        bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
    }

    public void setIcon(int resourceId) {
        this.bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        // 图片顶部居中显示
        int x = (this.getMeasuredWidth() - bitmap.getWidth()) / 2;
        int y = 0;
        canvas.drawBitmap(bitmap, x, y, null);
        // 坐标需要转换，因为默认情况下Button中的文字居中显示
        // 这里需要让文字在底部显示
        canvas.translate(0, (this.getMeasuredHeight() / 2) - (int) this.getTextSize());
        super.onDraw(canvas);
    }
}
